package ua.com.globallogic.training.task1;

public class FibonachiNumbers {
	private int countOfElements;
	private long[] fibonachiRow;
	
	public FibonachiNumbers(int countOfElements) {
		validateCountOfElements(countOfElements);
		this.countOfElements = countOfElements;
		fibonachiRow = new long[this.countOfElements];
		fibonachiRow[0] = 0;
		fibonachiRow[1] = 1;
		for (int i = 2; i < this.countOfElements; i++) {
			fibonachiRow[i] = fibonachiRow[i-1] + fibonachiRow[i-2];
		}
	}
	
	private void validateCountOfElements(int countOfElements) {
		if (countOfElements < 3) {
			System.out.println("\nCount of elements can not be less than 3\n");
			throw new IllegalArgumentException();
		}
	}
	
	public void printFibonachiRow() {
		for (int i = 0; i < fibonachiRow.length; i++) {
			if (i % 10 == 0 ) {
				System.out.println();
			}
			System.out.print(fibonachiRow[i] + "\t");
		}
	}
	
	public long getFibonachiRowElement(int elementNumber) {
		if (elementNumber < 1 || elementNumber >= fibonachiRow.length) {
			System.out.println("\nElement number cannot be less than 1 and more than " + fibonachiRow.length + "\n");
			throw new IllegalArgumentException();
		}
		return fibonachiRow[elementNumber - 1];
	}

}
